﻿using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class GutsRecord : BiffRecord
    {
        private ushort iLevelColMac;
        private ushort iLevelRwMac;
        private ushort unused1;
        private ushort unused2;

        public GutsRecord(Worksheet worksheet)
        {
            Contract.Requires(worksheet != null);
            iLevelRwMac = (ushort) ( Enumerable.Max(worksheet.Rows, r => r.OutlineLevel) );
            if (iLevelRwMac > 0)
                iLevelRwMac++;
            iLevelColMac = (ushort) ( Enumerable.Max(worksheet.Columns, r => r.OutlineLevel));
            if (iLevelColMac > 0)
                iLevelColMac++;
        }

        public GutsRecord()
        {
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Guts; }
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            unused1 = context.ReadUInt16();
            unused2 = context.ReadUInt16();
            iLevelRwMac = context.ReadUInt16();
            iLevelColMac = context.ReadUInt16();
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Writer.Write(unused1);
            context.Writer.Write(unused2);
            context.Writer.Write(iLevelRwMac);
            context.Writer.Write(iLevelColMac);
        }
    }
}